package com.kundi.dooradmin.controller;

import com.kundi.dooradmin.entity.Product;
import com.kundi.dooradmin.service.ProductService;
import com.kundi.dooradmin.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Author 赵恒
 * Date 2020/6/22
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductService productService;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Product> getAll() {
        return productService.getAll();
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, String> insert(Product product, @RequestBody MultipartFile file) throws IOException {
        product.setImage(file.getBytes());
        Integer i = productService.insert(product);
        if(i > 0) {
            return MapUtil.responseMap("添加成功", "success");
        } else {
            return MapUtil.responseMap("添加失败", "failed");
        }
    }

    @RequestMapping("/updateImage")
    @ResponseBody
    public Map<String, String> updateImage(Product product, @RequestBody MultipartFile file) throws IOException {
        product.setImage(file.getBytes());
        Integer i = productService.update(product);
        if(i > 0) {
            return MapUtil.responseMap("修改成功，刷新后可见", "success");
        } else {
            return MapUtil.responseMap("修改失败", "success");
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(Product product) {
        Integer i = productService.update(product);
        if(i > 0) {
            return MapUtil.responseMap("修改成功", "success");
        } else {
            return MapUtil.responseMap("修改失败", "success");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Integer i = productService.delete(id);
        if(i > 0) {
            return MapUtil.responseMap("删除成功", "success");
        } else {
            return MapUtil.responseMap("删除失败", "success");
        }
    }

    @RequestMapping("/getImage")
    public Object getAvatar(HttpServletRequest request, Integer id, String rand, HttpServletResponse response) throws IOException {
        Product product = productService.getProductByIdWithImage(id);
        response.setContentType("image/png");
        OutputStream output = response.getOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(product.getImage());
        int len;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            output.write(buf, 0, len);
        }
        output.flush();
        output.close();
        return null;
    }

    @RequestMapping("/getProductById")
    @ResponseBody
    public Product getProductById(Integer id) {
        return productService.getProductById(id);
    }

    @RequestMapping("/getFirstProduct")
    @ResponseBody
    public List<Product> getFirstProduct() {
        return productService.getFirstProduct();
    }
}
